// pages/home/home.js
const gd = getApp().globalData;
const net = require('../../../utils/net.js');
const util = require('../../../utils/util.js');
var posting = false;
var dialog = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: "../../../assets/image/logo.png",
    cardheight:0,
    list:[], 
    page:1,
    detail: {},
    scrollTop: 0,
    scrolltmp: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  godaka(){
    util.Forward("../daka/daka");
  }, gooverorder() {
    util.Forward("../order/order?type=2");
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    dialog = this.selectComponent("#dialog");
    this.getdetail();
  },
  getdetail() {
    if (posting){
      wx.stopPullDownRefresh();
      return;
    } 
    posting = true;
    var that = this;
    wx.showLoading({})
    net.request({
      url: '/MySport',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          that.setData({
            detail: dt.data
          })
        } else {
          dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {

      }, complete() {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
    posting = false;
    that.getlist();
  },
  getlist(){
      if (posting)return;
      posting = true;
      var that = this;
      wx.showLoading({})
      net.request({
        url: '/AllOrder',
        data: {
          shop_id: gd.shop_id,
          member_id: gd.id,
          page: that.data.page,
          search_day: util.formatDate(new Date())
        }, success(res) {
          //
          var dt = res.data;
          if (dt.flag) {
            if (dt.data.length > 0) {
              var tmplist = that.data.list;
              var _page =that.data.page+1;
              for (var i = 0; i < dt.data.length; ++i) {
                tmplist.push(dt.data[i]);
              }
              that.setData({
                page: _page,
                list: tmplist
              })
            } else {

            }
          } else {
            dialog.showDialog({
              msg: dt.msg
            })
          }
        }, fail(res) {

        }, complete() {
          wx.stopPullDownRefresh();
          posting = false;
          that.scrollloading(false);
          wx.hideLoading();
        }
      })
  },
  goorder(e){
    util.Forward("../order/order");
  },
  toscroll(e) {
    this.setData({
      scrolltmp: e.detail.scrollTop
    })
  }, tolower() {
    this.scrollloading(true);
    this.getlist();
  }, scrollloading(isshow) {
    var that = this;
    let top = 0;
    let delay = 0;
    if (isshow) {
      top = that.data.list.length * 60;
    } else {
      delay = 100;
      top = that.data.scrolltmp - 60;
    }
    setTimeout(function () {
      that.setData({
        scrollTop: top
      })
    }, delay);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      list:[]
    })
    this.getdetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})